package api;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import appmodel.JsonArrayLabel;
import appmodel.JsonModel;
import model.Confing;
import model.Lable;
import net.sf.json.JSONObject;
import service.ConfingService;
import service.IncomeService;
import service.LabelService;

@Controller
@RequestMapping({ "/" })
public class ConfingController {

	@Autowired
	private ConfingService confingSercvice;
	
	@Autowired
	private IncomeService incomeService;
	
	@Autowired
	private LabelService labelService;
	
	@RequestMapping(value = { "confing" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public JSONObject confing(HttpServletRequest request,JsonModel jsonModel) {
		Confing confing = this.confingSercvice.getConfing();
		if(null != confing) {
			jsonModel.setSuccess(true);
			jsonModel.setMsg("查询成功");
			jsonModel.setjData(confing);
			jsonModel.setToken("");
		}else {
			jsonModel.setSuccess(false);
			jsonModel.setMsg("查询失败");
			jsonModel.setjData(confing);
			jsonModel.setToken("");
		}
		
		return JSONObject.fromObject(jsonModel);
	}
	
	@RequestMapping(value = { "label" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public JSONObject getLabels(HttpServletRequest request,JsonModel jsonModel, HttpServletResponse response) {
		List<Lable> lables1 = this.labelService.getLabel1();
		List<Lable> lables2 = this.labelService.getLabel2();
		List<Lable> lables3 = this.labelService.getLabel3();
		
		response.addHeader("Access-Control-Allow-Origin", "*");
		response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
		response.setHeader("Access-Control-Allow-Headers", "x-requested-with, content-type");
		response.addHeader("Access-Control-Max-Age", "7200");// 120 min
		
		JsonArrayLabel arrayResult = new JsonArrayLabel();
		arrayResult.setLable1(lables1);
		arrayResult.setLable2(lables2);
		arrayResult.setLable3(lables3);
		
		if(null != lables1) {
			jsonModel.setSuccess(true);
			jsonModel.setMsg("查询成功");
			jsonModel.setjData(arrayResult);
			jsonModel.setToken("");
		}else {
			jsonModel.setSuccess(false);
			jsonModel.setMsg("查询失败");
			jsonModel.setjData(arrayResult);
			jsonModel.setToken("");
		}
		
		return JSONObject.fromObject(jsonModel);
	}
	
	
	//提现的接口 
	@RequestMapping(value = { "withdraw" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public JSONObject withdraw(HttpServletRequest request, JsonModel jsonModel,
			@RequestParam(value = "money", required = false) Long money,
			@RequestParam(value = "userId", required = false) Long userId) {
		
		Long incomeAll = incomeService.getIncomeAll(userId);
		if(incomeAll < money) {
			jsonModel.setSuccess(false);
			jsonModel.setMsg("提现金额不能大于余额");
			jsonModel.setjData("");
			jsonModel.setToken("");
		}else {
			Map<String,Object> map = new HashMap<>();
			map.put("income_number", -money);
			map.put("userId", userId);
			map.put("createDate", new Date());
			
			Integer result = incomeService.withDraw(map);

			if (null != result) {
				jsonModel.setSuccess(true);
				jsonModel.setMsg("提现成功");
				jsonModel.setjData("");
				jsonModel.setToken("");
			} else {
				jsonModel.setSuccess(false);
				jsonModel.setMsg("提现失败");
				jsonModel.setjData("");
				jsonModel.setToken("");
			}
		}
		
		return JSONObject.fromObject(jsonModel);
	}
	
	
	
}